package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

//curl -X POST "http://127.0.0.1:8080/user" -d "name=lcweng&age=25&address=gz"
//curl -H "Accept: application/json" -H "Content-Type:application/json" -X POST "http://127.0.0.1:8080/user" -d "{\"name\":\"lcweng\",\"age\":\"12\",\"address\":\"wuhan\"}"
//
func main() {
	r := gin.Default()
	r.POST("/user", bind)
	r.GET("/user", bind)
	_ = r.Run()
}

type Person struct {
	Name    string `json:"name" form:"name"`
	Age     string `json:"age" form:"age"`
	Address string `json:"address" form:"address"`
}

func bind(c *gin.Context) {
	var person = Person{}
	//shouldBind()根据不同contentType自动适配
	err := c.ShouldBind(&person)
	if err == nil { //错误为空即正常情况，可以使用person对象字段
		//c.String(200,"%s",person)
		c.JSON(http.StatusOK, gin.H{
			"userName": person.Name,
			"age":      person.Age,
			"addr":     person.Address,
		})
	} else {
		c.String(400, "person bind error", err)
	}
}
